<template>
<div></div>
</template>


<script lang="ts" setup>
import { useRoute } from 'vue-router'
import * as LoginApi from '@/api/login'
import * as authUtil from '@/utils/auth'
import router from '@/router'


// 获取路由实例
const route = useRoute()

// 从路由参数中获取authCode
const authCode = route.query.code as String| undefined
// 登录方法
const login = async (authCode: string | undefined) => {
  if (!authCode) {
    console.error('authCode is missing')
    return
  }

  try {
    const res = await LoginApi.dingLogin(authCode)
    authUtil.setToken(res)
    // 直接通过路由获取redirect参数，无需额外解析URL
    const redirect = route.query.redirect as string | undefined
    router.push({ path: redirect || '/' })
  } catch (error) {
    console.error('Login failed:', error)
    // 可以在这里添加登录失败的处理逻辑，比如提示用户
  }
}

// 组件加载时自动执行登录
if (authCode) {
  login(authCode)
}
</script>
